#include "detectFinger.h"

void* detectFinger(void* argument)
{
	argumentDataSet_t* argumentsData = (argumentDataSet_t*)argument;
	int x1, x2, y1, y2;
	int x, y, range;
	int xRange, yRange;
	coordinate_t triPoint;
	x1 = argumentsData->interestedArea.point1.x;
	y1 = argumentsData->interestedArea.point1.y;
	x2 = argumentsData->interestedArea.point2.x;
	y2 = argumentsData->interestedArea.point2.y;

	int theta;

	range = (x2
		- x1
		+ y2
		- y1) / 4
		* argumentsData->rangeRate;

	printf("range : %f\n", argumentsData->rangeRate);
	

	/* if not range < 0, then draw circle and enqueue */
	if (range < 0)
	{
		return NULL;
	}
	for (theta = 0; theta < 360; theta += 1)
	{
		triPoint.x = cos(theta * PI / 180.0)*range + argumentsData->point.x;
		triPoint.y = sin(theta * PI / 180.0)*range + argumentsData->point.y;
		

		int temp = (triPoint.y*WIDTH + triPoint.x) * 3;

		if (temp < 0)
		{
			temp = 0;
		}

		if (argumentsData->image->imageData[temp] == 255)
		{
			enqueue(argumentsData->unitQueue, 1, triPoint);
			argumentsData->image->imageData[temp] = 0;
			argumentsData->image->imageData[temp + 1] = 0;
			argumentsData->image->imageData[temp + 2] = 255;
		}
		else
		{
			enqueue(argumentsData->unitQueue, 0, triPoint);
			argumentsData->image->imageData[temp] = 0;
			argumentsData->image->imageData[temp + 1] = 255;
			argumentsData->image->imageData[temp + 2] = 0;
		}
	}

	int queue_length = argumentsData->unitQueue->front->data;

	
	/* queue clear */
	for (int i = 0; i < queue_length; i++)
	{
		int xx = argumentsData->unitQueue->front->next->point.x;
		int yy = argumentsData->unitQueue->front->next->point.y;
		int data = argumentsData->unitQueue->front->next->data;

		printf("%d", dequeue(argumentsData->unitQueue));
	}
	printf("\n");

	/* queue memory free */
	freeQueue(argumentsData->unitQueue);

	return NULL;
}

/* set arguments for finger thread */
void setArguments(	argumentDataSet_t* argumentsData,
					IplImage* image,
					queue_t* unitQueue,
					areaCoordinate_t interestedArea,
					coordinate point,
					float rangeRate)
{
	argumentsData->image = image;
	argumentsData->unitQueue = unitQueue;
	argumentsData->interestedArea = interestedArea;	
	argumentsData->point = point;	
	argumentsData->rangeRate = rangeRate;
}













